﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmoLiteApi.Sql;
using System.Data.SqlClient;
using SmoLiteApi.Helper;

namespace SmoLiteApi
{
    /// <summary/>
    public class Login
    {
        private Server _server;
        private string _name;

        /// <summary/>
        public Login(
            Server server,
            string name)
        {
            this._server = server;
            this._name = name;
        }

        /// <summary/>
        public string Name
        {
            get { return this._name; }
            set { this._name = value; }
        }

        /// <summary/>
        public void ChangePassword(string password)
        {
            LoginChangePasswordSql loginChangePasswordSql = new LoginChangePasswordSql();

            // set attributes
            loginChangePasswordSql.AddObject(new { Name = this._name, Password = password});

            using (SqlCommandHelper sqlCommandHelper = SqlCommandHelper.FetchSqlCommand(this._server.FetchSqlConnection(), loginChangePasswordSql.Render()))
            {
                // run the command
                sqlCommandHelper.ExecuteNonQuery();
            }
        }

        /// <summary/>
        public void Drop()
        {
            LoginDropSql loginDropSql = new LoginDropSql();

            // set attributes
            loginDropSql.AddObject(new { Name = this._name });

            using (SqlCommandHelper sqlCommandHelper = SqlCommandHelper.FetchSqlCommand(this._server.FetchSqlConnection(), loginDropSql.Render()))
            {
                // run the command
                sqlCommandHelper.ExecuteNonQuery();
            }
        }

        /// <summary/>
        public void Create(string password = null)
        {
            LoginCreateSql loginCreateSql = new LoginCreateSql();

            // set attributes
            loginCreateSql.AddObject(new { Name = this._name, Password = password });

            using (SqlCommandHelper sqlCommandHelper = SqlCommandHelper.FetchSqlCommand(this._server.FetchSqlConnection(), loginCreateSql.Render()))
            {
                // run the command
                sqlCommandHelper.ExecuteNonQuery();
            }
        }
    }
}
